home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group93a.txt
/
000084_icon-group-sender _Thu Mar 11 09:06:35 1993.msg
< prev
next >
Wrap
Internet Message Format
|
1993-04-21
|
3KB
Received: by cheltenham.cs.arizona.edu; Thu, 11 Mar 1993 13:40:37 MST
Date: Thu, 11 Mar 93 09:06:35 PST
From: alex@laguna.Metaphor.COM (Bob Alexander)
Message-Id: <9303111706.AA08783@laguna.Metaphor.COM>
To: icon-group@cs.arizona.edu
Subject: Re: Icon :-> C | ADA translator wanted.
Status: R
Errors-To: icon-group-errors@cs.arizona.edu
The ultimate Icon to C translation is performed by the Icon optimizing
compiler. However, as amazing as it is, the C it produces is not
something you would likely ever want to attempt to maintain as C code.
I've written several programs in Icon that I have later converted to C
for one reason or another (usually not speed, though), and found that
often it is often reasonable to just hand-change the various Icon
algorithms into C. But there are certain parts of that job that are
very annoying and have to be done over and over, like changing ":=" to
"=". So I wrote a little filter (in Icon, of course) that performs
some of the mundane tasks of hand-converting Icon to C.
It's pretty small, so I'm attaching it to this message. Maybe this will
prove useful to someone.
I have a similar little filter that converts in the other direction --
if anyone is interested let me know.
-- Bob Alexander
Metaphor Inc. (415) 966-0751 alex@metaphor.com
====^=== Mountain View, CA ...{uunet}!{decwrl,apple}!metaphor!alex
-------------------------- icn2c.icn -----------------------------------
#
# Program to do some mundane aspects of conversion of Icon to C.
#
# - Reformats comments
# - Reformats line-continued strings
# - Changes := to =
# - Reformats procedure declarations
# - Changes end to "}"
#
procedure main(arg)
parenLevel := 0
while line := trim(read(),' \t') do line ? {
line := comment := suffix := ""
="procedure" & tab(many(' \t')) & suffix := " {"
="end" & tab(many(' \t')) | pos(0) & line ||:= "}"
while line ||:= tab(upto('\'":#')) do {
case c := move(1) of {
"\"" | "'": {
#
# Handle character strings.
#
line ||:= c
repeat {
until line ||:= tab(find(c) + 1) do {
line ||:= tab(0)
if line[-1] == "_" then line[-1] := "\""
else stop("unbalanced quotes")
Out(line)
line := ""
&subject := read()
line := (tab(many(' \t')) | "") || "\""
}
if not (line[-2] == "\\" & not (line[-3] == "\\")) then break
}
}
"#": {
#
# Handle comments.
#
comment := trim(tab(0),' \t')
}
":": {
#
# Change := to =
#
if ="=" then line ||:= "="
else line ||:= c
}
"(": {
parenLevel +:= 1
line ||:= c
}
")": {
parenLevel -:= 1
line ||:= c
}
default: line ||:= c
}
}
line ||:= tab(0) || suffix
tline := trim(line,' \t')
if not (parenLevel > 0 | *tline = 0 |
any('{}(!%&*+,-./:<=>?@\\^',tline,-1) |
(tline[-4:0] == ("else" | "then") &
not tline[-5] | any(' \t',tline[-5]))) then {
line := tline || ";" || line[*tline + 1:0]
}
Out(line,comment)
}
end
procedure Out(line,comment)
line ||:= "/*" || ("" ~== \comment) || " */"
line := trim(line,' \t')
write(line)
return
end